package com.yiwenup.leetcode.top_interview_150;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-09 22:09:49
 * @Description https://leetcode.cn/problems/spiral-matrix-ii/
 */
public class No0059 {
    public int[][] generateMatrix(int n) {

        int[][] ans = new int[n][n];
        int level = 1;
        int count = 1;
        int x = 0;
        int y = 0;
        while (level <= n / 2) {
            while (y < n - level) {
                ans[x][y++] = count++;
            }
            while (x < n - level) {
                ans[x++][y] = count++;
            }
            while (y >= level) {
                ans[x][y--] = count++;
            }
            while (x >= level) {
                ans[x--][y] = count++;
            }

            x++;
            y++;
            level++;
        }

        if (n % 2 == 1) {
            ans[x][y] = count;
        }

        return ans;
    }
}
